<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/5/28
 * Time: 14:21
 */

namespace app\admin\library;


class JsonAnalysis
{

    /**
     * 解析json获取模块api地址
     * @param string $json
     * @return array|mixed
     */
    public static function moduleApi($json='')
    {
        $data = [];
        if ($json){
            $data = json_decode($json,true);
            if ($data){
                $data = $data['path'];
            }
        }
        return $data;
    }

    /**
     * 解析获取模块列表静态查询条件
     * @param $data
     * @return array
     */
    public static function staticCondition($data)
    {
        $result = [];
        foreach ($data as $value){
            $item = [];
            $json_data = json_decode($value['search_static'], true) ?? $value['$value'];
            if (is_array($json_data)){
                $item['name'] = $value['name'];
                $item['option'] = $json_data['option'];
                $item['value'] = $json_data['value'];
            }else{
                $item['name'] = $value['name'];
                $item['option'] = $json_data;
            }

            array_push($result,$item);
        }
        return $result;
    }

    /**
     * @param $column
     * @param string $table_name
     * @return array
     * @throws \think\exception\DbException
     */
    public static function viewCreate($column, $table_name = '')
    {
        $columnValidateModel = new \app\admin\model\MwColumnValidate();
        $result = [];
        if (is_array($column)){
            foreach ($column as $value){
                $data =[];
                if (!empty($table_name)){
                    $data['module'] = $table_name;
                }
                $data['name'] = $value['name'];
                $data['description'] = $value['description'];
                $data['type'] = $value['output_type'];
                $data['multiple'] = !empty($value['is_multiple']) ? true : false;
                $data['search'] = !empty($value['search']) ? true : false;
                $data['default'] = $value['default'];
                if ($data['search']){//存在搜索项则提取搜索规则
                    $data['rule'] = $value['search_rule'];
                    if ($value['option']){
//                        $data['option'] = self::optionData($search_rule['option']);
                        $data['option'] = json_decode($value['option'], true) ?? $value['option'];
                    }
                }
                //字段验证
                $validateList = $columnValidateModel->where('column_id',$value['id'])->select();
                if ($validateList){
                    $validate = [];
                    foreach ($validateList as $val){
                        $item = [];
                        $item['validator'] = $val['type'];
                        $item['trigger'] = $val['trigger'];
                        if ($val['type'] == 'remote'){
                            $item['api'] = json_decode($val['condition'], true) ?? $val['condition'];
                        }elseif (json_decode($val['condition'], true)) {
                            $item['extra'] = json_decode($val['condition'], true);
                        }

                        array_push($validate, $item);
                    }
                    $data['validate'] = $validate;
                }

                array_push($result,$data);
            }
        }

        return $result;
    }

    public static function optionData($option)
    {
        $result = [];
        switch ($option['type']) {
            case 'api':
                $result['type'] = 'api';
                $result['url'] = $option['url'];
                break;
            case 'static':
                $result['type'] = 'static';
                $result['data'] = $option['data'];
                break;
        }
        return $result;
    }
}